﻿<?php

/**
 * This is the model class for table "tbl_users".
 *
 * The followings are the available columns in table 'tbl_users':
 * @property integer $id
 * @property string $name
 * @property string $username
 * @property string $email
 * @property string $password
 * @property string $usertype
 * @property integer $block
 * @property integer $sendEmail
 * @property string $registerDate
 * @property string $lastvisitDate
 * @property string $activation
 * @property string $forgotten_password_code
 * @property string $params
 *
 * The followings are the available model relations:
 * @property Bidjob[] $bidjobs
 * @property Comment[] $comments
 * @property Resume[] $resumes
 * @property Skills[] $skills
 * @property UserLogs[] $userLogs
 * @property UserProfiles[] $userProfiles
 * @property UserStatistics[] $userStatistics
 * @property Usergroups[] $tblUsergroups
 */
class Users extends CActiveRecord
{
	public $password_repeat;
	public $verifyCode;
	private  $checkCaptcha = true;
	private  $checkDangduan = true;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Users the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_users';
	}
	
	public function getCaptcha()
	{
		if ($this->checkCaptcha){
			return !CCaptcha::checkRequirements();
		}else 
			return CCaptcha::checkRequirements();
	}
	public function getDangduan()
	{
		if ($this->checkDangduan){
			return 'required';
		}else
			return '';
	}
	
	public function setCheckCaptcha($on){
		$this->checkCaptcha = $on;
	}
	public function setCheckDangduan($on){
		$this->checkDangduan = $on;
	}
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			// So sanh pass và re-pass khi dang ky
			array('password','compare','compareAttribute'=>'password'),
				array('password_repeat','safe'), 
			// So sanh username dang ky voi username da ton tai trong csdl
			array('username', 'unique'), 
			array('name, username, email, password, password_repeat, usertype, block, sendEmail, registerDate, lastvisitDate, activation, forgotten_password_code', 'required'),
			// Email has to be a valid email address
			array('email', 'email', 'pattern'=>"/^[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&\'*+\\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/"), 
			array('block, sendEmail', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'max'=>255),
			array('username', 'length', 'min' => 3, 'max'=>150),
			// Dong hoa chu thuong va chu HOA de save vao CSDL
			array('username','filter','filter'=>'strtolower'), 
			array('email, password, activation', 'length', 'max'=>100),
			array('usertype', 'length', 'max'=>25),
			array('forgotten_password_code', 'length', 'max'=>40),
			array('verifyCode', 'captcha', 'allowEmpty'=>$this->getCaptcha()),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		      'jobs' => array(self::HAS_MANY, 'Job', 'user_id'),
			'bidjobs' => array(self::HAS_MANY, 'Bidjob', 'user_id'),
			'comments' => array(self::HAS_MANY, 'Comment', 'user_id'),
			'resumes' => array(self::HAS_MANY, 'Resume', 'user_id'),
			'skills' => array(self::HAS_MANY, 'Skills', 'user_id'),
			'userLogs' => array(self::HAS_MANY, 'UserLogs', 'user_id'),
			'userProfiles' => array(self::HAS_MANY, 'UserProfiles', 'user_id'),
			'userStatistics' => array(self::HAS_MANY, 'UserStatistics', 'user_id'),
			'tblUsergroups' => array(self::MANY_MANY, 'Usergroups', 'tbl_user_usergroup_map(user_id, group_id)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Username Display',
			'username' => 'Username',
			'email' => 'Email Address',
			'password' => 'Password',
			'password_repeat' => 'Confirm Password',
			'usertype' => 'Usertype',
			'block' => 'Block',
			'sendEmail' => 'Send Email',
			'registerDate' => 'Register Date',
			'lastvisitDate' => 'Lastvisit Date',
			'activation' => 'Activation',
			'forgotten_password_code' => 'Mã reset Password',
			'params' => 'Purpose join website',
			'verifyCode'=>'Verification Code',
		);
	}
	
	/**
	 * perform one-way encrytion before we store it in the database
	 */
	protected function beforeValidate(){
        if ($this->isNewRecord) {
            $this->usertype = 'member';
            $this->block    = 1;
            $this->sendEmail = 1;
            $this->registerDate = new CDbExpression('NOW()');
            $this->lastvisitDate = $this->registerDate;
            $this->activation = 'activation';
            $this->forgotten_password_code = rand(1,150000) + "bodd.biz";
        }
        return parent::beforeValidate();
    }
	
	/**
	 * perform one-way encrytion on the password before we store it in the database
	 */
	protected function afterValidate() {
		parent::afterValidate();
		$this->password = $this->encrypt($this->password);
	}
		
	public function encrypt($value) {
		return md5($value);
	}
	
}